//
//  SinaTabBarController.m
//  MySinaWeiboProjectForOC
//
//  Created by apple on 2016/11/12.
//  Copyright © 2016年 WangChen. All rights reserved.
//

#import "SinaTabBarController.h"
#import "SinaNaviBaseController.h"
#import "SinaTabBar.h"
#import "SinaHomeComposeView.h"
#import "UIImage+extension.h"
#import "UIImage+ImageEffects.h"

@interface SinaTabBarController ()

@end

@implementation SinaTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //自定义tabBar
    SinaTabBar *myTabBar = [[SinaTabBar alloc]init];
    
    //点击撰写按钮
    myTabBar.composeBtnDidClick = ^{
        
        [self composeBtnDidClick];
        
    };
    
    [self setValue:myTabBar forKey:@"tabBar"];
    
    [self addNaviControllers];
    
    [self addTag];
    
}

#pragma mark
#pragma mark - 点击撰写按钮触发方法
-(void)composeBtnDidClick{
    
    SinaHomeComposeView *composeView = [[SinaHomeComposeView alloc]init];
    [self.view addSubview:composeView];
    
    [composeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    UIImage *image = [UIImage imageWithScreen];
    UIImage *effectImage = [image applyLightEffect];
    
    composeView.backImage = effectImage;
}



#pragma mark
#pragma mark - 添加所有NaviVC
- (void)addNaviControllers{
    
    SinaNaviBaseController *homeNavi = [self addNaviControllerWithVcName:@"SinaHomeViewController" title:@"主页" imageName:@"tabbar_home" tag:0];
    SinaNaviBaseController *messageNavi = [self addNaviControllerWithVcName:@"SinaMessageViewController" title:@"消息" imageName:@"tabbar_message_center" tag:1];
    SinaNaviBaseController *discoverNavi = [self addNaviControllerWithVcName:@"SinaDiscoverViewController" title:@"发现" imageName:@"tabbar_discover" tag:2];
    SinaNaviBaseController *profileNavi = [self addNaviControllerWithVcName:@"SinaProfileViewController" title:@"我的" imageName:@"tabbar_profile" tag:3];
    
    self.viewControllers = @[homeNavi,messageNavi,discoverNavi,profileNavi];
    
}


#pragma mark
#pragma mark - 添加1个naviVC
- (SinaNaviBaseController *)addNaviControllerWithVcName:(NSString*)vcName title:(NSString *)title imageName:(NSString *)imageName tag:(NSInteger)tag{
    
    Class cls = NSClassFromString(vcName);
    UIViewController *vc = [[cls alloc]init];
    vc.title = title;
    
    vc.tabBarItem.tag = tag;
    
    [vc.tabBarItem setImage:[[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [vc.tabBarItem setSelectedImage:[[UIImage imageNamed:[NSString stringWithFormat:@"%@_selected",imageName]]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    
    [vc.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, -3.0)];
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor orangeColor]} forState:UIControlStateSelected];
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor grayColor]} forState:UIControlStateNormal];
    
    SinaNaviBaseController *navi = [[SinaNaviBaseController alloc]initWithRootViewController:vc];
    
    
    return navi;
}


//添加tag
-(void)addTag{
    NSInteger index = 0;
    for (UIView* subView in self.tabBar.subviews) {
        if([subView isKindOfClass:NSClassFromString(@"UITabBarButton")]){
            subView.tag = index;
            index ++;
        }
    }
}


-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    
    NSInteger tag = item.tag;
    
    for (UIView* subView in self.tabBar.subviews) {
        if(subView.tag == tag && [subView isKindOfClass:NSClassFromString(@"UITabBarButton")]){
            for (UIView *theSub in subView.subviews) {
                if([theSub isKindOfClass:NSClassFromString(@"UIImageView")]){
                    UIImageView *iv = (UIImageView *)theSub;
                    iv.transform = CGAffineTransformMakeScale(0.5, 0.5);
                    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.3 initialSpringVelocity:7 options:0 animations:^{
                        iv.transform = CGAffineTransformIdentity;
                    } completion:nil];
                }
            }
        }
    }
    
}


@end
